/**
 * 
 */
package name.pathfinder.tuberose.example.actions;

import name.pathfinder.tuberose.example.entities.User;
import name.pathfinder.tuberose.example.services.UserService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.dispatcher.DefaultActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


/**
 * @author yaowei
 *
 */
@Namespace("/main")
@Controller
public class MainAction extends DefaultActionSupport {
	
	private String message ;
	private boolean showMessage = false;
	
	private Long userId;
	
	private User user;
//
	@Autowired
	private UserService userService;

	/**
	 * 
	 */
	private static final long serialVersionUID = 3715490602226294616L;
	
	
	@Action(value="home")
	public String index() {
		User user = null;
		if(null != userId) {
			user = userService.findUser(userId);
		}
		if(null != user) {
			setMessage("Hello " + user.getUsername() + "!");
			setShowMessage(true);
		}
		return SUCCESS;
	}
	
	@Action(value="save", results = { @Result(name = "success", location = "home", type = "redirectAction") })
	public String saveUser() {
		if(null != user) {
			userService.saveUser(user);
			return SUCCESS;
		}
		return ERROR;
	}


	public void setMessage(String message) {
		this.message = message;
	}


	public String getMessage() {
		return message;
	}


	public void setShowMessage(boolean showMessage) {
		this.showMessage = showMessage;
	}


	public boolean isShowMessage() {
		return showMessage;
	}


	/**
	 * @return the userId
	 */
	public Long getUserId() {
		return userId;
	}


	/**
	 * @param userId the userId to set
	 */
	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

}
